package com.wlw.admin.openeye.ui.fragment

import android.os.Bundle
import android.support.v7.widget.LinearLayoutManager
import com.wlw.admin.openeye.R
import com.wlw.admin.openeye.base.BaseFragment
import com.wlw.admin.openeye.mvp.contract.RankContract
import com.wlw.admin.openeye.mvp.model.bean.HomeBean
import com.wlw.admin.openeye.mvp.presenter.RankPresenter
import com.wlw.admin.openeye.net.exception.ErrorStatus
import com.wlw.admin.openeye.showToast
import com.wlw.admin.openeye.ui.adapter.CategoryDetailAdapter
import kotlinx.android.synthetic.main.layout_recyclerview.*

class RankFragment : BaseFragment(), RankContract.View {
    private var itemList = ArrayList<HomeBean.Issue.Item>()
    private val mAdapter by lazy {
        activity?.let { CategoryDetailAdapter(it, itemList) }
    }
    private val mPresenter by lazy {
        RankPresenter()
    }

    init {
        mPresenter.attachView(this)
    }

    override fun getLayoutId(): Int = R.layout.layout_recyclerview

    override fun lazyLoad() {
        arguments?.getString("url")?.let { mPresenter.requestRankList(it) }
    }

    override fun initView() {
        mRecyclerView.layoutManager = LinearLayoutManager(activity)
        mRecyclerView.adapter = mAdapter
    }

    override fun setRankList(itemList: ArrayList<HomeBean.Issue.Item>) {
        multipleStatusView.showContent()
        mAdapter?.addData(itemList)
    }

    override fun showError(errorMsg: String, errorCode: Int) {
        showToast(errorMsg)
        multipleStatusView.showError()
        if (errorCode == ErrorStatus.NETWORK_ERROR)
            multipleStatusView.showNoNetwork()
        else
            multipleStatusView.showError()
    }

    override fun showLoading() {
        multipleStatusView.showLoading()
    }

    override fun dismissLoading() {
        multipleStatusView.showContent()
    }

    override fun onDestroy() {
        super.onDestroy()
        mPresenter.detachView()
    }

    companion object {
        fun getInstance(string: String): RankFragment {
            val fragment = RankFragment()
            val bundle = Bundle()
            bundle.putString("url", string)
            fragment.arguments = bundle
            return fragment
        }
    }
}